<?php
namespace app\console\controller;

use app\console\model\Role;
use app\console\model\Manager as ManagerModel;
use think\Validate;
use think\Request;

class Manager extends Base
{
    public function index()
    {
        $admins = ManagerModel::alias('a')
            ->join('roles r', 'a.role_id = r.id','LEFT')
            ->field('a.*, r.name as role_name')
            ->order('a.id ASC')
            ->paginate(20);
    	// dump(json_decode( json_encode( $admins),true));exit;


        return view('index', compact('admins'));
    }

    public function add()
    {   
    	$roles = Role::field('id, name')->order('id DESC')->select();
        return view('add', compact('roles'));
    }

    public function addPost()
    {
        $validate = new Validate(
            [
                ['role_id','require','角色不能为空'],
                ['name','require|unique:admins','账号不能为空|该账号已存在，请重新输入'],
                ['password','require','密码不能为空'],
            ]
        );
        $data = $this->request->param();
        if (!$validate->check($data)) {
            $res['title'] = $validate->getError();
            $res['type'] = 'error';
            return success($res);
        }
        $data['password'] = MD5($data['password']);
        $admin = new ManagerModel();
        $admin->allowField(true)->save($data);
        set_log(2, '新增管理员账号：'. $data['name']);

        $res['url'] = url('console/manager/index');
        $res['title'] = '新增管理员成功';
        $res['type'] = 'success';

        return success($res);
    }

    public function edit($id)
    {
        $detail = ManagerModel::find($id);
        $roles = Role::field('id, name')->order('id DESC')->select();

        return view('edit', compact('detail', 'roles'));
    }

    public function editPost()
    {
        $validate = new Validate(
            [
                ['role_id','require','角色不能为空'],
                ['name','require','账号不能为空'],
            ]
        );
        $data = $this->request->param();
        if (!$validate->check($data)) {
        	$res['title'] = $validate->getError();
            $res['type'] = 'error';
            return success($res);
        }
        $admin_info = ManagerModel::find($data['id']);
        if($data['password']){
        	$admin_info->password = md5($data['password']);
        }
        $admin_info->name = $data['name'];
        $admin_info->role_id = $data['role_id'];
        $admin_info->save();

        set_log(2, '编辑管理员账号：'. $data['name']. '的信息');

        $res['url'] = url('console/manager/index');
        $res['title'] = '更新管理员成功';
        $res['type'] = 'success';

        return success($res);
    }

    public function delete($id)
    {
        set_log(2, '删除ID为：'.$id.' 的管理员账号');
        ManagerModel::destroy($id);
        // return redirect('index');
        $res['url'] = url('console/manager/index');
        $res['title'] = '删除成功';
        $res['type'] = 'success';

        return success($res);
    }

    public function uppassword()
    {
        if (Request::instance()->isPost()){
            $validate = new Validate(
                [
                    ['password1','require','新密码不能为空'],
                    ['password2','require|confirm:password1','确认新密码不能为空|两次密码输入不一致'],
                ]
            );
            $data = $this->request->param();
            if (!$validate->check($data)) {
                $res['title'] = $validate->getError();
                $res['type'] = 'error';
                return success($res);
            }
            $password = MD5($data['password2']);
            $res = ManagerModel::where('id', $this->user->id) ->update(['password' => $password]);
            if ($res){
                $res2['url'] = url('console/manager/uppassword');
                $res2['title'] = '修改密码成功';
                $res2['type'] = 'success';
                return success($res2);
            }

        } else{
            $mannager = $this->user;

            return view('uppassword', compact('mannager'));
        }
    }
}
